package boardgamer.yavalath.client.mainmenu;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.RectF;
import boardgamer.common.client.Drawable;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.yavalath.client.LetterHex;
import boardgamer.yavalath.client.RenderHelper;
import com.google.common.collect.ImmutableSet;

/**
 * @author garysoed
 */
public class NewTitle implements HasLayout, Drawable {

  private final ImmutableSet<LetterHex> hexes;
  private final LayoutGroup group;

  public NewTitle() {
    ImmutableSet.Builder<LetterHex> hexesBuilder = ImmutableSet.builder();

    PointF position = new PointF();
    hexesBuilder.add(createHex('Y', Color.rgb(255, 0, 0), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    hexesBuilder.add(createHex('A', Color.rgb(237, 0, 36), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexUpRightDisplacement());
    hexesBuilder.add(createHex('V', Color.rgb(219, 0, 72), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    hexesBuilder.add(createHex('A', Color.rgb(201, 0, 108), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexUpRightDisplacement());
    hexesBuilder.add(createHex('L', Color.rgb(183, 0, 144), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    hexesBuilder.add(createHex('A', Color.rgb(165, 0, 180), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexUpRightDisplacement());
    hexesBuilder.add(createHex('T', Color.rgb(147, 0, 216), position));

    position = offsetPosition(position, RenderHelper.instance().getHHexDownRightDisplacement());
    hexesBuilder.add(createHex('H', Color.rgb(129, 0, 252), position));

    this.hexes = hexesBuilder.build();
    this.group = new LayoutGroup();
    for (LetterHex hex : hexes) {
      this.group.add(hex).atCurrentRect();
    }
  }

  private LetterHex createHex(char letter, int hexColor, PointF position) {
    return new LetterHex(letter, hexColor, position, RenderHelper.instance().getHexLetterPaint(),
        false /* isVertical */);
  }

  private PointF offsetPosition(PointF original, PointF offset) {
    return new PointF(original.x + offset.x, original.y + offset.y);
  }

  @Override
  public void draw(Canvas canvas) {
    for (LetterHex hex : hexes) {
      hex.draw(canvas);
    }
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }
}
